﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace CS3230HealthTrackerSystemGUI
{
    public partial class UpdateTestForm : Form
    {

        /* Creates the variables to hold the information for the current patient 
         * to be registered.
         */
        private String testDate;
        private String testTime;
        private String patientID;
        private String nurseID;
        private String resultDate;
        private String testResult;
        private String diagnosis;
        private String appointmentTime;
        private Validation validate = new Validation();
        private Decryption key = new Decryption();
        string MyConString;

        public UpdateTestForm()
        {
            InitializeComponent();
            MyConString = MyConString = key.returnConnectionString("xxjrmy", "xxjrmy", "xx3kz4z");
        }

        private void UpdateTestCheckButton_Click(object sender, EventArgs e)
        {
            //Gathers data from each of the text boxes and verify the data is valid. 
          
            patientID = updateTestPatientIDTextBox.Text;

            //Validates all the fields.
            validate.validateID(patientID, "Patient");
            
                
            //Creates statement to insert all the information into the database. 
            string checkForTestInfo = "SELECT * FROM `TEST` WHERE `TEST`.patientTested=" + patientID;
            executeCommand(checkForTestInfo, "Searching . . .");

        }

        private void updateTestUpdateButton_Click(object sender, EventArgs e)
        {
            try
            {
                //Gathers data from each of the text boxes and verify the data is valid. 
                testDate = updateTestDateTextBox.Text;
                testTime = updateTestTimeTextBox.Text;
                patientID = updateTestPatientIDTextBox.Text;
                nurseID = updateTestNurseTextBox.Text;
                resultDate = updateTestResultDateTextBox.Text;
                testResult = updateTestResultTextBox.Text;
                diagnosis = updateDiagnosisTextBox.Text;
                appointmentTime = appointmentTimeTextBox.Text;

                //Validates all the fields.
                validate.validateID(patientID, "Patient");               
                validate.validateDate(testDate);
                validate.validateTime(testTime);
                validate.validateID(nurseID, "Nurse");
                validate.validateDate(resultDate);
                validate.validateTestResults(testResult);
                validate.validateDiagonosis(diagnosis);
                validate.validateTime(appointmentTime);

                //Converts all the information into the appropriate data types.
                int currentPatient = Int32.Parse(patientID);
                int currentNurse = Int32.Parse(nurseID);

                //Updates Test Results.
                string updateTestResults = "UPDATE `TEST` SET `testResult`='" + testResult + "', `dateResultRecieved`='" + resultDate + "', `nurseRecording`='" + nurseID + "'" +
                                           "WHERE `TEST`.dateTestPerformed='" + testDate + "' AND `TEST`.timeTestPerformed='" + testTime + "' AND `TEST`.patientTested='" + patientID + "'";
                executeCommand(updateTestResults, "Test Updated.");
                //Updates diagnosis for a visit.
                string updateVisit = "UPDATE `VISIT` SET `diagnosis`='" + diagnosis + "' WHERE `VISIT`.patientVisiting ='" + patientID + "' AND `VISIT`.appointmentDate='" + testDate +
                                     "' AND `VISIT`.checkInTime='" + appointmentTime + "'";
                executeCommand(updateVisit, "Diagnosis Recorded");
                
            }

             //Throws an exception if something is incorrectly formatted.
            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }


        }

        private void CheckVisitInfo_Click(object sender, EventArgs e)
        {
            //Gathers data from each of the text boxes and verify the data is valid. 
         
            patientID = updateTestPatientIDTextBox.Text;

            //Validates all the fields.
            validate.validateID(patientID, "Patient");
           

            string checkForTestInfo = "SELECT * FROM `VISIT` WHERE `VISIT`.patientVisiting=" + patientID;
            executeCommand(checkForTestInfo, "Searching . . .");
        }

        private void executeCommand(string commandline, string completeMessage){

            try
            {


                //Converts all the information into the appropriate data types.
                int currentPatient = Int32.Parse(patientID);

                //Assigns Connection string.
                OdbcConnection dataAccess = new OdbcConnection(MyConString);

                MessageBox.Show(completeMessage);
                //Holds the values to be stored into the dataGridview to be displayed.
                List<StringHolder> list = new List<StringHolder>();

                OdbcConnection MyConnection = new OdbcConnection(MyConString);

                MyConnection.Open();

                OdbcCommand MyCommand = new OdbcCommand(commandline, MyConnection);
                OdbcDataReader MyDataReader;
                MyDataReader = MyCommand.ExecuteReader();


                while (MyDataReader.Read())
                {
                    //Creates a string to hold the values from the query.
                    String builder = " ";
                    //Checks the number of columns that are going to be readin, and for each column.
                    int values = MyDataReader.VisibleFieldCount;
                    for (int currentColumn = 0; currentColumn < values; currentColumn++)
                    {
                        //If the value in the current column is null.
                        if (MyDataReader.IsDBNull(currentColumn) == true)
                        {
                            builder += " " + MyDataReader.GetName(currentColumn) + " : " + " NULL ";
                        }
                        else
                        {
                            //Checks for the type of data in the column, and handles it appropriately.
                            //For each it checks the type, gets the column name, and then the value.
                            String type = MyDataReader.GetDataTypeName(currentColumn);

                            if (type == "varchar")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetString(currentColumn).ToString() + " ";
                            }
                            if (type == "integer")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetInt32(currentColumn).ToString() + " ";
                            }
                            if (type == "decimal")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDecimal(currentColumn).ToString() + " ";
                            }
                            if (type == "date")
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDate(currentColumn).ToShortDateString() + " ";
                            }
                        }
                    }
                    //Stores the string in the string wrapper.
                    StringHolder currentString = new StringHolder(builder);
                    //Adds the string to be added into the data grid view.
                    list.Add(currentString);
                }
                //Displays the data in the datagridview.
                dataDisplay.DataSource = list;
                dataDisplay.Show();

                //Close all resources
                MyDataReader.Close();
                MyConnection.Close();

            }

           //Throws an exception if something is incorrectly formatted.
            catch (Exception formatException)
            {
                MessageBox.Show(formatException.Message);
            }
        }
    }
}
